package cn.internship.modules.manage.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.internship.common.utils.Constant;
import cn.internship.modules.manage.entity.StudentInfoEntity;
import cn.internship.modules.manage.entity.dto.WeeklyRecordSearch;
import cn.internship.modules.manage.service.StudentInfoService;
import cn.internship.modules.sys.controller.AbstractController;
import cn.internship.modules.sys.entity.SysUserEntity;
import cn.internship.modules.sys.entity.SysUserRoleEntity;
import cn.internship.modules.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.internship.modules.manage.entity.WeeklyRecordEntity;
import cn.internship.modules.manage.service.WeeklyRecordService;
import cn.internship.common.utils.PageUtils;
import cn.internship.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author haha
 * @email haha@gmail.com
 * @date 2023-02-20 15:58:08
 */
@RestController
@RequestMapping("manage/weeklyrecord")
public class WeeklyRecordController extends AbstractController {
    @Autowired
    private WeeklyRecordService weeklyRecordService;

    @Resource
    private StudentInfoService studentInfoService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("manage:weeklyrecord:list")
//    public R list(@RequestParam Map<String, Object> params){
//        //只有超级管理员，才能查看所有管理员列表
//        if(getUserId() != Constant.SUPER_ADMIN){
//            params.put("userId", getUserId());
//        }
//
//        PageUtils page = weeklyRecordService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("manage:weeklyrecord:list")
    public R list(WeeklyRecordSearch search){
        //获取用户ID
        Long userId = getUserId();
        //通过用户ID查询角色
        LambdaQueryWrapper<SysUserRoleEntity> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(SysUserRoleEntity::getUserId,userId);
        SysUserRoleEntity one = sysUserRoleService.getOne(queryWrap);
        if (one == null){
            return R.error("该用户不属于任何角色");
        }
        if (one.getRoleId() == 2){
            search.setTeaUserId(userId);
        }
        if (one.getRoleId() == 3){
            search.setStuUserId(userId);
        }

        PageUtils page = weeklyRecordService.listByPage(search);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:weeklyrecord:info")
    public R info(@PathVariable("id") Long id){
		WeeklyRecordEntity weeklyRecord = weeklyRecordService.getById(id);

        return R.ok().put("weeklyRecord", weeklyRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:weeklyrecord:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody WeeklyRecordEntity weeklyRecord){
        LambdaQueryWrapper<StudentInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentInfoEntity::getUserId,getUserId());
        StudentInfoEntity studentInfo = studentInfoService.getOne(queryWrapper);
        weeklyRecord.setStuId(Long.parseLong(studentInfo.getId().toString()));
        weeklyRecord.setCreateTime(new Date());
        weeklyRecordService.save(weeklyRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:weeklyrecord:update")
    public R update(@RequestBody WeeklyRecordEntity weeklyRecord){
		weeklyRecordService.updateById(weeklyRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:weeklyrecord:delete")
    public R delete(@RequestBody Long[] ids){
		weeklyRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 评分
     */
    @RequestMapping("/score")
    @RequiresPermissions("manage:weeklyrecord:score")
    public R score(@RequestBody WeeklyRecordEntity weeklyRecord){
        weeklyRecordService.updateById(weeklyRecord);

        return R.ok();
    }

}
